package com.mh.service.impl;

import com.mh.controller.OrderInfosController;
import com.mh.dto.R;
import com.mh.dto.vo.OrderShowVo;
import com.mh.pojo.GoodsInfos;
import com.mh.pojo.OrderInfos;
import com.mh.service.OrderInfosService;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Date:2024/10/29
 * author:zmh
 * description: 订单信息实现类
 **/

@Service
@RequiredArgsConstructor
@Slf4j
public class OrderInfosServiceImpl implements OrderInfosService {

    private final MongoTemplate  mongoTemplate;

    @Override
    public R<List<OrderShowVo>> getAllOrderByUserId(String userId) {
        Query query = new Query().addCriteria(Criteria.where("userId").is(userId));
        List<OrderInfos> orderInfos = mongoTemplate.find(query, OrderInfos.class);

        // 根据商品Id获取商品信息，封装Dto返回
        ArrayList<OrderShowVo> orderShowVos = new ArrayList<>();
        for(OrderInfos orderInfo: orderInfos ){
            OrderShowVo orderShowVo = new OrderShowVo();
            BeanUtils.copyProperties(orderInfo, orderShowVo);

            Query goodsQuery = new Query().addCriteria(Criteria.where("goodsId").is(orderInfo.getGoodsId()));
            GoodsInfos goodsInfo = mongoTemplate.findOne(goodsQuery, GoodsInfos.class);
            orderShowVo.setGoodsInfos(goodsInfo);

            // 将封装好的Vo对象加到返回的数组中
            orderShowVos.add(orderShowVo);
        }

        return R.success(orderShowVos);
    }

    @Override
    public R<Map<Integer, Long>> getOrderStatusCount(String userId) {
        Query query = new Query().addCriteria(Criteria.where("userId").is(userId));
        List<OrderInfos> orderInfos = mongoTemplate.find(query, OrderInfos.class);

        // 创建返回的结构
        Map<Integer, Long> statusCountMap = new HashMap<>();

        for (OrderInfos order : orderInfos) {
            int status = order.getOrderStatus();
            statusCountMap.put(status, statusCountMap.getOrDefault(status, 0L) + 1);
        }
        return R.success(statusCountMap);
    }

    /**
     * 根据订单ID获取订单信息
     * @param orderId ��ID
     * @return ·
     */
    @Override
    public R<OrderInfos> getOrderInfoById(String orderId) {
        Query query = new Query().addCriteria(Criteria.where("orderId").is(orderId));
        OrderInfos orderInfos = mongoTemplate.findOne(query, OrderInfos.class);
        return R.success(orderInfos);
    }

    /**
     * 根据订单ID取消订单
     * @param orderId 订单IDD
     * @return ·
     */
    @Override
    public R<String> cancelOrder(String orderId) {
        Query query = new Query().addCriteria(Criteria.where("orderId").is(orderId));
        Update update = new Update();
        update.set("orderStatus", 5);
        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, OrderInfos.class);
        if (updateResult.getMatchedCount() > 0) {
            return R.success("订单取消成功");
        }
        return R.error("订单取消失败");
    }

    /**
     * 根据订单ID删除订单
     * @param orderId 订单ID
     * @return ·
     */
    @Override
    public R<String> deleteOrder(String orderId) {
        Query query = new Query().addCriteria(Criteria.where("orderId").is(orderId));
        DeleteResult remove = mongoTemplate.remove(query, OrderInfos.class);
        if (remove.getDeletedCount() > 0) {
            return R.success("订单删除成功");
        }
        return R.error("订单删除失败");
    }
}
